﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Mgrs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Assets.Scripts.DL.ComponentScripts {
	public class DiCi : MonoBehaviour {
		[SerializeField] BoxCollider2D coll;
		[SerializeField] Animator animator;
		[SerializeField] int damage;

		public void Init(Vector3 pos) {
			transform.position = pos;
			animator.Play("Show");
			showMark = true;
			timer ??= new();
			end = false;
			sendDmg = false;
		}
		bool showMark;
		bool end;
		Timer timer;
		public void Update() {
			if (animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.5 && showMark) {
				showMark = false;
				coll.enabled = true;
				timer.Reset();
			}
			if (!showMark && timer.Elapsed > 2) {
				coll.enabled = false;
				animator.Play("End");
				timer.Reset();
				end = true;
			}

			if (end && timer.Elapsed > 1) {
				ObjectPool.Instance.RecycleObject(ObjPoolStr.DiCi, gameObject);
			}
		}

		bool sendDmg;
		private void OnTriggerEnter2D(Collider2D collision) {
			if (!sendDmg && collision.CompareTag(Tags.Player)) {
				collision.GetComponent<PlayerController>().TakeDamage(damage);
				sendDmg = true;
			}
		}
	}
}
